package com.tgy.JUC.CollectionUnSafe;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

/***
 * @ClassName: MapUnsafe
 * @Description: HashMap不安全实例
 * @Auther: 送你一块二向箔
 * @Date: 14:20 2022/3/11
 * @version : V1.0
 */
public class MapUnsafe {
    public static void main(String[] args) {
        //unsafe();
        safe();
    }

    public static void safe(){
        // 默认相当于
        Map<String, String> map = new ConcurrentHashMap<>(16, 0.75F);
        for (int i = 0; i < 10; i++) {
            new Thread(()->{
                map.put(Thread.currentThread().getName(), UUID.randomUUID().toString().substring(0,5));
                System.out.println(map);
            }).start();
        }
    }

    public static void unsafe(){

        Hashtable<Object, Object> objectObjectHashtable = new Hashtable<>();
        // 默认相当于
        Map<String, String> map = new HashMap<>(16, 0.75F);
        for (int i = 0; i < 10; i++) {
            new Thread(()->{
                map.put(Thread.currentThread().getName(), UUID.randomUUID().toString().substring(0,5));
                System.out.println(map);
            }).start();
        }
    }
}
